﻿#region Summary

// 
// Owner : JackieHan 
// 
// Solution : Pharyngitis 
// 
// Project : TTrip.Pharyngitis.NHibernate
// 
// File : NHiberanteExtension.cs
// 
// CopyRight : Copyright © 2010-2011 Skomart Corporation, All Rights Reserved 
// 
// Summary : NHibernate扩展
// 
// History : 
//			2012-03-21 18:54:11	    Create 
// 

#endregion

#region Using

using System;
using System.Collections.Generic;
using System.Linq;
using NHibernate;
using NHibernate.Engine;
using NHibernate.Linq;

#endregion

namespace Jackie.Utility.NHibernate
{
    ///<summary>
    ///NHibernate扩展
    ///</summary>
    public static class NHiberanteExtension
    {
        /// <summary>
        /// 创建NHiberante Linq 查询
        /// </summary>
        /// <typeparam name="T">查询类型</typeparam>
        /// <param name="session">扩充Session</param>
        /// <returns></returns>
        public static IQueryable<T> Query<T>(this ISession session)
        {
            return new NhQueryable<T>(session as ISessionImplementor);
        }

        /// <summary>
        /// 保存批量对象
        /// </summary>
        /// <typeparam name="T">需要保存的类型</typeparam>
        /// <param name="session">扩充对象Session</param>
        /// <param name="objects">需要保存的对象集合</param>
        /// <param name="validate">验证方法</param>
        public static void SaveObjects<T>(this ISession session, IEnumerable<T> objects, Action<T> validate)
        {
            foreach (T tObj in objects)
            {
                validate(tObj);
                session.SaveOrUpdate(tObj);
            }
        }
    }
}